package top.hkyzf.study.leetcode.array;

/**
 * description: 
 * @author zhufeng
 * @date 2024/3/18 0018 9:28
 * @version V1.0
 */
class NumArray {
    private int[] arr;
    public NumArray(int[] nums, int bak) {
        this.arr = nums;
    }

    public int sumRange(int left, int right, int bak) {
        int sum = 0;
        for (int i = left; i <= right; i++) {
            sum += arr[i];
        }
        return sum;
    }


    private int[] preSum;
    public NumArray(int[] nums) {
        preSum = new int[nums.length + 1];
        preSum[0] = nums[0];
        for (int i = 1; i <= nums.length; i++) {
            preSum[i] = preSum[i-1] + nums[i-1];
        }
    }

    public int sumRange(int left, int right) {
        return preSum[right+1] - preSum[left];
    }

}
